#!/usr/bin/python
"""
Python shell for SfePy.

   This is just a normal Python shell (IPython shell if you have the
   IPython package installed),  that executes the following commands
   for the user:

       >>> from sfepy.base.base import *
       >>> from sfepy.fem import *
       >>> from sfepy.applications import solve_pde
       >>> from sfepy.postprocess import Viewer

   So starting 'isfepy' is roughly (*) equivalent to starting Python (or
   IPython) and executing the above commands by hand. It is intended for easy
   and quick experimentation with SfePy.

   (*) Advantages of using isfepy instead of IPython directly are:
       - all SfePy messages are logged into ~/.sfepy/isfepy.log;
       - it is possible to silence the messages printed on screen.

   COMMAND LINE OPTIONS
   --------------------

   -c CONSOLE, --console=CONSOLE

     Use the specified Python or IPython shell as console backend instead
     of the default one (IPython if present or Python otherwise), e.g.:

        isfepy -c python

   -q, --quiet

     Print only Python's and SfePy's versions to stdout at startup.

   -s, --silent

     Silence the messages printed to screen.

   -- IPython's options

     Additionally you can pass command line options directly to IPython
     interpreter (standard Python shell is not supported).  However you
     need to add '--' separator between two types of options. To run
     SfePy without startup banner and colors, for example, issue:

        isfepy -q -- -colors NoColor
        isfepy -q -- --colors NoColor (for ipython >= 0.11)

     For non-blocking matplotlib plots in ipython >= 0.11 use:

        isfepy -- --pylab=wx
"""
from optparse import OptionParser

import sfepy

usage = 'usage: isfepy [options] -- [ipython options]'
help = {
    'console' :
    'select type of interactive session: ipython | python [default: %default]',
    'quiet' :
    'print only version information at startup',
    'silent' :
    'silence the messages printed on screen',
    'no_viewer' :
    'do not attempt to import Viewer',
    'no_wx' :
    'do not try using the threaded Wx ipython shell',
}

def main():
    parser = OptionParser(usage=usage, version="%prog " + sfepy.__version__)
    parser.add_option('-c', '--console', dest='console', action='store',
                      default='ipython', choices=['ipython', 'python'],
                      help=help['console'])
    parser.add_option('-q', '--quiet', dest='quiet', action='store_true',
                      default=False, help=help['quiet'])
    parser.add_option('-s', '--silent', dest='silent', action='store_true',
                      default=False, help=help['silent'])
    parser.add_option('', '--no-viewer', dest='is_viewer', action='store_false',
                      default=True, help=help['no_viewer'])
    parser.add_option('', '--no-wx', dest='is_wx', action='store_false',
                      default=True, help=help['no_wx'])
    options, ipy_args = parser.parse_args()

    session = options.console
    ipython = session == 'ipython'

    args = {
        'argv'   : ipy_args,
        'quiet' : options.quiet,
        'silent' : options.silent,
        'is_viewer' : options.is_viewer,
        'is_wx' : options.is_wx,
    }

    from sfepy.interactive import init_session
    init_session(ipython, **args)

if __name__ == "__main__":
    main()
